import {Component, EventEmitter, OnInit, Output} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {NzMessageService} from 'ng-zorro-antd/message';
import {CandidateService} from '../../service/candidate.service';
import {SharedModule} from '../shared.module';
import {CandidateForm} from '../../types/req/CandidateForm';
import {MODAL_WIDTH} from '../../const'
import areas from '../../const/area.json'

@Component({
  selector: 'candidate-add-of-job-seeker',
  imports: [
    SharedModule
  ],
  templateUrl: './candidate-add-of-job-seeker.component.html',
  standalone: true
})
export class CandidateAddOfJobSeekerComponent implements OnInit {
  candidateToSave:CandidateForm = {};
  candidateForm!: FormGroup;
  isVisible: boolean = false
  saveLoading: boolean = false
  title: string = '新增应聘者基本信息'
  provinceCityAreaOptions = areas.provinces;

  @Output() addedEvent = new EventEmitter<boolean>();

  constructor(
    private fb: FormBuilder,
    private message: NzMessageService,
    private candidateService: CandidateService
  ) {}

  ngOnInit(): void {
    this.candidateForm = this.fb.group({
      username: ['', [Validators.required]],
      name: ['', [Validators.required]],
      password: ['', [Validators.required]],
      gender: [undefined, [Validators.required]],
      birthDate: ['', [Validators.required]],
      contactPhone: ['', [Validators.required]],
      email: ['', [Validators.required]],
      province: [''],
      city: [''],
      area: [''],
      provinceCityArea: [[]],
      address: [''],
    })

    this.display(1)
  }

  onSave() {
    try {
      this.saveLoading = true
      for (const i in this.candidateForm.controls) {
        this.candidateForm.controls[i].markAsDirty();
        this.candidateForm.controls[i].updateValueAndValidity();
      }
      if (!this.candidateForm.valid) {
        this.saveLoading = false
        return
      }
      this.candidateService.save(Object.assign(this.candidateToSave, this.candidateForm.value)).subscribe({
        next: (resp) => {
          if (!resp || resp.code !== 1) {
            this.message.error(resp && resp.msg ? resp.msg : '操作异常')
            return
          }
          this.message.success("保存成功")
        },
        complete: () => {
          this.saveLoading = false
        }
      })
    } catch (e) {
      console.log('获取数据异常', e)
    }
  }

  onReset() {
    this.title = '新增应聘者基本信息'.slice()
    this.candidateForm.reset()
  }

  display(id: number) {
    this.isVisible = true
    if (id) {
      this.title = '修改应聘者基本信息'
      this.saveLoading = true
      this.candidateService.findById(id).subscribe({
        next: (resp) => {
          if (resp && resp.code === 1 && resp.data) {
            this.candidateToSave = resp.data
            const provinceCityArea = []
            if (this.candidateToSave.province) {
              provinceCityArea.push(this.candidateToSave.province)
            }
            if (this.candidateToSave.city) {
              provinceCityArea.push(this.candidateToSave.city)
            }
            if (this.candidateToSave.area) {
              provinceCityArea.push(this.candidateToSave.area)
            }
            this.candidateForm.patchValue(Object.assign({provinceCityArea: provinceCityArea},this.candidateToSave));
          }
        },
        complete: () => {
          this.saveLoading = false
        }
      })
    } else {
      this.onReset()
    }
  }
}
